//
//  CollectionViewController.m
//  afn
//
//  Created by mac on 2018/5/10.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "CollectionViewController.h"
#import "CollectionView.h"
#import "CollectionModel.h"
@interface CollectionViewController ()
@property(nonatomic,strong)CollectionView *aView;
@property(nonatomic,strong)UIView *bottomView;
@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的收藏";
    
    _aView=[CollectionView new];
    [self.view addSubview:_aView];
    [_aView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(NAV_HEIGHT);
    }];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(BianJi:)];
    _aView.isSelect=NO;
    _aView.tableArr1=[NSMutableArray arrayWithCapacity:0];
    _aView.tableArr2=[NSMutableArray arrayWithCapacity:0];
    _aView.selectArr=[NSMutableArray arrayWithCapacity:0];
    _aView.abc=1;
    [_aView creatView];
    _aView.viewController=self;

}

-(void)BianJi:(UIBarButtonItem *)btn{
    if ([btn.title isEqualToString:@"编辑"]) {
        
        _aView.scrollView.scrollEnabled = NO;
        _aView.shopBtn.userInteractionEnabled=NO;
        _aView.serviceBtn.userInteractionEnabled=NO;
        if (_aView.abc==1) {
            
            [_aView.shopTabelView setEditing:YES animated:YES];
            [_aView.shopTabelView setFrame:CGRectMake(0, 0,_mainW ,_mainH-NAV_HEIGHT-45-50)];
            
        }
        else{
            [_aView.serviceTableView setEditing:YES animated:YES];
            [_aView.serviceTableView setFrame:CGRectMake(_mainW, 0,_mainW ,_mainH-NAV_HEIGHT-45-50)];
        }
        _aView.isSelect=YES;
        [self creatSelecBtn];
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(BianJi:)];
    }
    else{
        _aView.shopBtn.userInteractionEnabled=YES;
        _aView.serviceBtn.userInteractionEnabled=YES;
        _aView.scrollView.scrollEnabled = YES;
        if (_aView.abc==1) {
            [_aView.shopTabelView setEditing:NO animated:YES];
            [_aView.shopTabelView setFrame:CGRectMake(0, 0,_mainW ,_mainH-NAV_HEIGHT-45)];
            
        }
        else{
            [_aView.serviceTableView setEditing:NO animated:YES];
            [_aView.serviceTableView setFrame:CGRectMake(_mainW, 0,_mainW ,_mainH-NAV_HEIGHT-45)];
            
        }
        [_aView.selectArr removeAllObjects];
        _aView.isSelect=NO;
        [_bottomView removeFromSuperview];
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(BianJi:)];
    }
    
    
}

-(void)creatSelecBtn{
    _bottomView=[UIView new];
    [self.view addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    _bottomView.backgroundColor=[UIColor whiteColor];
    
    UIView *xianView=[UIView new];
    [_bottomView addSubview:xianView];
    [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    xianView.backgroundColor=_LightGrey;
    
    UIButton *btn=[UIButton new];
    [_bottomView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.mas_equalTo(0);
        make.width.mas_equalTo(_mainW/2);
    }];
    [btn setTitleColor:ColorWithRGB(149, 149, 149) forState:0];
    [btn setTitle:@"全选" forState:0];
    btn.titleLabel.font=[UIFont systemFontOfSize:16];
    [btn addTarget:self action:@selector(qunXuan:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn2=[UIButton new];
    [_bottomView addSubview:btn2];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.top.mas_equalTo(0);
        make.width.mas_equalTo(_mainW/2);
    }];
    btn2.backgroundColor=ColorWithRGB(251, 83, 86);
    [btn2 setTitleColor:[UIColor whiteColor] forState:0];
    [btn2 setTitle:@"删除" forState:0];
    btn2.titleLabel.font=[UIFont systemFontOfSize:16];
    [btn2 addTarget:self action:@selector(shanchu) forControlEvents:UIControlEventTouchUpInside];
}
-(void)qunXuan:(UIButton *)btn{
    if ([btn.titleLabel.text isEqualToString:@"全选"]) {
        [btn setTitle:@"取消全选" forState:0];
        if (_aView.abc==1) {
            for (int i = 0; i < _aView.tableArr1.count; i++) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [_aView.shopTabelView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
                
                CollectionModel *model=[CollectionModel mj_objectWithKeyValues:_aView.tableArr1[i]];
                [_aView.selectArr addObject:model];
            }
        }
        else{
            for (int i = 0; i < _aView.tableArr2.count; i++) {
                
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [_aView.serviceTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
                CollectionModel *model=[CollectionModel mj_objectWithKeyValues:_aView.tableArr2[i]];
                [_aView.selectArr addObject:model];
                
            }
        }
        
    }
    else{
        [btn setTitle:@"全选" forState:0];
        if (_aView.abc==1) {
            for (int i = 0; i < _aView.tableArr1.count; i++) {
                
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [_aView.shopTabelView deselectRowAtIndexPath:indexPath animated:YES];
                CollectionModel * model=[CollectionModel mj_objectWithKeyValues:_aView.tableArr1[i]];
                [_aView.selectArr removeObject:model];
            }
        }
        else{
            for (int i = 0; i < _aView.tableArr2.count; i++) {
                
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [_aView.serviceTableView deselectRowAtIndexPath:indexPath animated:YES];
                CollectionModel * model=[CollectionModel mj_objectWithKeyValues:_aView.tableArr2[i]];
                [_aView.selectArr removeObject:model];
            }
        }
    
    }
}
-(void)shanchu{
    NSMutableArray *typeArr=[NSMutableArray arrayWithCapacity:0];
    for (int i=0; i<_aView.selectArr.count; i++) {
        CollectionModel *mode=_aView.selectArr[i];
        [typeArr addObject:mode.collection_id];
    }    
    NSData *data=[NSJSONSerialization dataWithJSONObject:typeArr options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    

    
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:jsonStr,@"collection_id", nil];
    [gongju AFNPOSTDataUrl:@"v1/collection/collection-array-delete" andDic:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
        if ([responseObject boolValue]) {
            [gongju svpInfo:@"删除成功"];
            
        }else{
            [gongju svpInfo:@"删除失败"];
            return ;
        }
        self->_aView.scrollView.scrollEnabled = YES;
        if (self->_aView.abc==1) {
            [self->_aView.shopTabelView setEditing:NO animated:YES];
            [self->_aView.shopTabelView setFrame:CGRectMake(0, 0,_mainW ,_mainH-NAV_HEIGHT-45)];
            [self->_aView.tableArr1 removeAllObjects];
            
        }
        else{
            [self->_aView.serviceTableView setEditing:NO animated:YES];
            [self->_aView.serviceTableView setFrame:CGRectMake(_mainW, 0,_mainW ,_mainH-NAV_HEIGHT-45)];
            [self->_aView.tableArr2 removeAllObjects];
        }
        [self->_aView.selectArr removeAllObjects];
        self->_aView.isSelect=NO;
        [self->_bottomView removeFromSuperview];
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(BianJi:)];
        [self->_aView LoginDic1];
        
        
    } anderrorBlock:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
